package com.google.code.opengw.http;

import java.util.ArrayList;
import java.util.List;


public class Request {
	private List<HTTPHeader> httpHeaderList;
	private List<HTTPCookie> httpCookieList;
	private List<HTTPParameter> httpParamList;
	private HTTPMethod method;
	private String protocol;
	private String uri;
	private String host;
	private String xForwardedFor;
	private boolean keepAlive;
	private int readTimeout;
	private int connectionTimeout;
	private int idleTimeout;
	private int contentLength;
	private byte[] content;
	
	public Request(String clientIp){
		httpCookieList = new ArrayList<HTTPCookie>();
		httpHeaderList = new ArrayList<HTTPHeader>();
		httpParamList = new ArrayList<HTTPParameter>();
		contentLength = -1;
		xForwardedFor = clientIp;
	}
	
	public Request(String host, String clientIp){
		this(clientIp);
		this.host = host; 
	}
	
	public String getHTTPHeaderString(boolean withParam){
		
		StringBuilder httpRequest = new StringBuilder();
		httpRequest.append(method).append(" ").append(uri);
		if(withParam){
			httpRequest.append("?");
			for(HTTPParameter param : httpParamList){
				httpRequest.append(param.getParameterLine());
				httpRequest.append("&");
			}
			httpRequest.deleteCharAt(httpRequest.length() - 1);
		}
		httpRequest.append(" ").append(protocol).append("\r\n");
		
		for(HTTPHeader header : httpHeaderList){
			httpRequest.append(header.toHeaderLine());
		}
		if(keepAlive){
			httpRequest.append("Connection: Keep-Alive\r\n");
		}
		httpRequest.append("X-Forwarded-For: ");
		httpRequest.append(xForwardedFor);
		httpRequest.append("\r\n");
		
		if(!httpCookieList.isEmpty()){
			httpRequest.append("Cookie:");
			for(HTTPCookie cookie : httpCookieList) {
				httpRequest.append(" ");
				httpRequest.append(cookie.getName());
				httpRequest.append("=");
				httpRequest.append(cookie.getValue());
				httpRequest.append(";");
			}
			httpRequest.deleteCharAt(httpRequest.length() - 1);
			httpRequest.append("\r\n");
		}
		
		httpRequest.append("\r\n");
		
		return httpRequest.toString();
		
	}
	
	public void addHeader(HTTPHeader header){
		if(header.getName().equalsIgnoreCase("Content-Length")){
			contentLength = Integer.parseInt(header.getValue());
		}
		
		if(header.getName().equalsIgnoreCase("Host")) {
			httpHeaderList.add(new HTTPHeader("Host", host));
		} else if(header.getName().equalsIgnoreCase("Connection") && header.getValue().equalsIgnoreCase("Keep-Alive")) {
			keepAlive = true;
		} else if (header.getName().equalsIgnoreCase("Cookie")){
			String[] cookieLineArr = header.getValue().split(";");
			for(String cookieLine : cookieLineArr)
				httpCookieList.add(new HTTPCookie(cookieLine.trim()));
		} else if(header.getName().equalsIgnoreCase("X-Forwarded-For")){
			xForwardedFor += ", " + header.getValue();
		} else
			httpHeaderList.add(header);
			
		
	}
	
	public void addParams(String paramStr){
		String[] paramList = paramStr.split("&");
		for(String paramLine : paramList)
			httpParamList.add(new HTTPParameter(paramLine));
	}
	
	public String getProtocol() {
		return protocol;
	}
	public void setProtocol(String protocol) {
		this.protocol = protocol;
	}
	public String getUri() {
		return uri;
	}
	public void setUri(String uri) {
		this.uri = uri;
	}
	public List<HTTPHeader> getHttpHeaderList() {
		return httpHeaderList;
	}
	public void setHttpHeaderList(List<HTTPHeader> httpHeaderList) {
		this.httpHeaderList = httpHeaderList;
	}
	public List<HTTPCookie> getHttpCookieList() {
		return httpCookieList;
	}
	public void setHttpCookieList(List<HTTPCookie> httpCookieList) {
		this.httpCookieList = httpCookieList;
	}
	public HTTPMethod getMethod() {
		return method;
	}
	public void setMethod(HTTPMethod method) {
		this.method = method;
	}
	public boolean isKeepAlive() {
		return keepAlive;
	}
	public void setKeepAlive(boolean keepAlive) {
		this.keepAlive = keepAlive;
	}
	public int getReadTimeout() {
		return readTimeout;
	}
	public void setReadTimeout(int readTimeout) {
		this.readTimeout = readTimeout;
	}
	public int getConnectionTimeout() {
		return connectionTimeout;
	}
	public void setConnectionTimeout(int connectionTimeout) {
		this.connectionTimeout = connectionTimeout;
	}
	public int getIdleTimeout() {
		return idleTimeout;
	}
	public void setIdleTimeout(int idleTimeout) {
		this.idleTimeout = idleTimeout;
	}
	public int getContentLength() {
		return contentLength;
	}
	public void setContentLength(int contentLength) {
		this.contentLength = contentLength;
	}
	public byte[] getContent() {
		return content;
	}
	public void setContent(byte[] content) {
		this.content = content;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}
	
	
}
